package nu.member.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nu.board.model.GroupVO;
import nu.board.model.MemberGroupVO;
import nu.member.model.MemberVO;
import nu.member.model.NickVO;
import nu.member.service.MemberService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MemberController {
	@Resource(name="memberService")
	private MemberService service;
	
	@RequestMapping("login.nu")
	public ModelAndView loginMember(HttpSession session, MemberVO vo) throws Exception{
		String result="";
		MemberVO rvo = service.login(vo);
		if(rvo!=null){
			service.setNickName(vo);
			rvo = service.getMemberInfo(vo);
			List<GroupVO>list=service.getGroupListByMember(vo);
			rvo.setList(list);
			session.setAttribute("login", rvo);
			result="redirect:/menu.nu";
		}else{
			result="login_fail";
		}
		return new ModelAndView(result);
	}
	@RequestMapping("logout.nu")
	public ModelAndView logoutMember(HttpSession session)throws Exception{
		if(session!=null){
			if(session.getAttribute("login")!=null){
				session.invalidate();
			}
		}
		return new ModelAndView("logout");
	}
	@RequestMapping("menu.nu")
	public ModelAndView menu()throws Exception{
		return new ModelAndView("menu");
	}
	@RequestMapping("registerMember.nu")
	public String registerMember(HttpServletRequest request, HttpServletResponse response, HttpSession session, MemberVO paramMvo) throws Exception{
		MemberVO vo = new MemberVO(request.getParameter("registerId"),request.getParameter("registerPass"),paramMvo.getUploadfile());
		MemberVO rvo=service.registerMember(vo);
		String result="";
		if(rvo!=null){
			service.setNickName(vo);
			rvo = service.getMemberInfo(vo);
			List<GroupVO>list=service.getGroupListByMember(vo);
			rvo.setList(list);
			session.setAttribute("login", rvo);
			result="redirect:/menu.nu";
		}else{
			result="register_fail";
		}
		return result;
	}
	@RequestMapping("myInfoPage.nu")
	public ModelAndView myInfo(HttpSession session) throws Exception{
		String result="";
		if(session.getAttribute("login")!=null){//redirect
			MemberVO mvo = (MemberVO)session.getAttribute("login");
			NickVO nvo = 
					new NickVO(service.getNickContent(mvo.getStudy_nick(),"study"),
							service.getNickContent(mvo.getSmoking_nick(),"smoking"),
							service.getNickContent(mvo.getSports_nick(),"sports"),
							service.getNickContent(mvo.getFinance_nick(),"finance"),
							service.getNickContent(mvo.getLiving_nick(),"living"));
			System.out.println(nvo);
			return new ModelAndView("myInfo/myInfoPage","nvo",nvo);
		}else{
			return new ModelAndView("index");
		}
	}
	
	@RequestMapping("updatePage.nu")
	public String updatePage() throws Exception{
		return "myInfo/updatePage";
	}
	
	@RequestMapping("updateMember.nu")
	public String updateMember(HttpServletRequest request, HttpServletResponse response,HttpSession session,MemberVO paramMvo) throws Exception{
		String result="";
		MemberVO vo = new MemberVO(request.getParameter("updateId"),request.getParameter("updatePass"),paramMvo.getUploadfile());
		MemberVO rvo = service.updateMember(vo);
		if(rvo!=null){
			service.setNickName(vo);
			rvo = service.getMemberInfo(vo);
			List<GroupVO>list=service.getGroupListByMember(vo);
			rvo.setList(list);
			session.setAttribute("login", rvo);
			result="redirect:/myInfoPage.nu";
		}else{
			result="update_fail";
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("idCheck.nu")
	public int idCheck(String id) throws Exception{
		//System.out.println(id);
		int result = service.idCheck(id);
		return result;
	}
	@RequestMapping("joinGroup.nu")
	public ModelAndView joinGroup(HttpSession session, MemberGroupVO mgvo) throws Exception{
		if(session!=null){		//session 다시 set
			if(session.getAttribute("login")!=null){
				boolean flag=true;
				MemberVO mvo = (MemberVO)session.getAttribute("login");
				List<GroupVO> list = service.getGroupListByMember(mvo);
				for(int i=0;i<list.size();i++){
					if(list.get(i).getGroup_type().equals(mgvo.getGroup_type())){
						flag=false;
						break;
					}
				}
				if(flag){
					int result=service.joinGroup(mgvo);
					List<GroupVO> list2 = service.getGroupListByMember(mvo);
					mvo.setList(list2);
					session.setAttribute("login", mvo);
				}
			}
		}
		return new ModelAndView("redirect:/showGroupList.nu?category="+mgvo.getGroup_type());
	}
}
